// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index';
import allRouter from './router/allRouter';
import defaultRouter from './router/defaultRouter';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import Print from "./utils/print";
Vue.use(Print);
Vue.use(ElementUI, {
  size: 'medium'
  // i18n: (key, value) => i18n.t(key, value)
})
// Vue.use(MyPrint);
//注册
Vue.config.productionTip = false;
console.log(process.env.NODE_ENV)
let registerRouteFresh = true;
// 通过路由守卫来修改页面title
router.beforeEach(async (to, from, next) => {
  console.log(store.state.userInfo, 'vuex');
  if (to.path == '/web-view') next();
  else {
    if (store.state.userInfo || store.state.userInfo == 0) {
      if (registerRouteFresh) {

        const routers = defaultRouter;
        if (store.state.userInfo == 0) {
          routers[0].children = allRouter;

        } else {
          store.state.routers.split(",").forEach(i => {
            const routeItem = allRouter.find(t => {
              return i === t.path;
            });
            if (routeItem) routers[0].children.push(routeItem);
          });
        }
        console.log(store.state.allRouter, 'allrouter');

        await store.commit('updatedallRouter', routers);
        // await router.$addRoute(store.state.allRouter[0].children);
        registerRouteFresh = false;
        console.log(router, "===>");
        // return to.fullPath;
        await router.addRoute(store.state.allRouter[0]);
        next({ ...to, replace: true });
      }
    } else {
      if (to.path !== '/login') next('/login');

    }
    if (to.meta.title) {
      document.title = to.meta.title;
    }
  }


  // if (store.state.userRole.length === 0) {
  //   const userRole = ['module2_xuanxiang1'];   //模拟的匹配规则
  //   store.state.userRole = userRole
  //   store.dispatch('getRouter', userRole).then(() => {
  //     const routers = store.state.routers;  //匹配后的路由
  //     console.log(routers, '还在吗');
  //     routers.forEach((i, ind) => {
  //       router.addRoute(i);
  //     })
  //     next();
  //     console.log(router, '路由');
  //   })
  // }



  next();
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
